//
//  UIView+GG.m
//  unzip
//
//  Created by yg on 2021/11/15.
//

#import "UIView+GG.h"
#import <objc/runtime.h>

@implementation UIView (GG)

//absoluteFrame:相对于屏幕坐标系的frame
- (CGRect)gg_absoluteFrame{
	return [self convertRect:self.bounds toView:nil];
}

- (CGFloat)gg_left {
	return self.frame.origin.x;
}
- (void)setGg_left:(CGFloat)gg_x {
	CGRect frame = self.frame;
	frame.origin.x = gg_x;
	self.frame = frame;
}

- (CGFloat)gg_top {
	return self.frame.origin.y;
}

- (void)setGg_top:(CGFloat)gg_top {
	CGRect frame = self.frame;
	frame.origin.y = gg_top;
	self.frame = frame;
}

- (CGFloat)gg_right {
	return self.frame.origin.x + self.gg_width;
}
- (void)setGg_right:(CGFloat)gg_right {
	CGRect frame = self.frame;
	frame.origin.x = gg_right - frame.size.width;
	self.frame = frame;
}

- (CGFloat)gg_bottom {
	return self.frame.origin.y + self.frame.size.height;
}

- (void)setGg_bottom:(CGFloat)gg_bottom {
	CGRect frame = self.frame;
	frame.origin.y = gg_bottom - frame.size.height;
	self.frame = frame;
}

- (CGFloat)gg_centerX {
	return self.center.x;
}

- (void)setGg_centerX:(CGFloat)gg_centerX {
	self.center = CGPointMake(gg_centerX, self.center.y);
}

- (CGFloat)gg_centerY {
	return self.center.y;
}

- (void)setGg_centerY:(CGFloat)gg_centerY {
	self.center = CGPointMake(self.center.x, gg_centerY);
}

- (CGFloat)gg_width {
	return self.frame.size.width;
}

- (void)setGg_width:(CGFloat)gg_width {
	CGRect frame = self.frame;
	frame.size.width = gg_width;
	self.frame = frame;
}

- (CGFloat)gg_height {
	return self.frame.size.height;
}

- (void)setGg_height:(CGFloat)gg_height {
	CGRect frame = self.frame;
	frame.size.height = gg_height;
	self.frame = frame;
}

- (CGPoint)gg_origin {
	return self.frame.origin;
}

- (void)setGg_origin:(CGPoint)gg_origin {
	CGRect frame = self.frame;
	frame.origin = gg_origin;
	self.frame = frame;
}

- (CGSize)gg_size {
	return self.frame.size;
}

- (void)setGg_size:(CGSize)gg_size {
	CGRect frame = self.frame;
	frame.size = gg_size;
	self.frame = frame;
}


/**
 移除所有子视图
 */
- (void)gg_removeAllSubviews {
	[self.subviews performSelector:@selector(removeFromSuperview)];
}


/**
 当前View的ViewController
 */
- (UIViewController *)gg_viewController {
	for (UIView *view = self; view; view = view.superview) {
		UIResponder *nextResponder = [view nextResponder];
		if ([nextResponder isKindOfClass:[UIViewController class]]) {
			return (UIViewController *)nextResponder;
		}
	}
	return nil;
}

/**
 *  @brief  view截图
 *
 *  @return 截图
 */
- (UIImage *)gg_screenshotImage {
	UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
	if( [self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
	{
		[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
	}
	else
	{
		[self.layer renderInContext:UIGraphicsGetCurrentContext()];
	}
	
	UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return screenshot;
}

/** 切圆角 */
-(void)gg_cornerRadius: (CGFloat)radius
		borderWidth: (CGFloat)width
			  color: (UIColor *_Nullable)color
{
	self.layer.masksToBounds = YES;
	self.layer.cornerRadius = radius;
	self.layer.borderColor = color.CGColor;
	self.layer.borderWidth = width;
}

/**
 *  设置阴影
 */
-(void)gg_shadowWithColor: (UIColor *)color
				offset: (CGSize)offset
			   opacity: (CGFloat)opacity
				radius: (CGFloat)radius
{
	self.clipsToBounds = NO;
	self.layer.shadowColor = color.CGColor;
	self.layer.shadowOffset = offset;
	self.layer.shadowOpacity = opacity;
	self.layer.shadowRadius = radius;
}


@end


static char kActionHandlerTapBlockKey;
static char kActionHandlerTapGestureKey;
static char kActionHandlerLongPressBlockKey;
static char kActionHandlerLongPressGestureKey;

@implementation UIView (GGGestures)

- (void)gg_addTapActionWithBlock:(GestureActionBlock)block{
	
	UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerTapGestureKey);
	if (!gesture)
	{
		gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForTapGesture:)];
		[self addGestureRecognizer:gesture];
		objc_setAssociatedObject(self, &kActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
	}
	objc_setAssociatedObject(self, &kActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);
}

- (void)handleActionForTapGesture:(UITapGestureRecognizer*)gesture{
	
	if (gesture.state == UIGestureRecognizerStateRecognized)
	{
		GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerTapBlockKey);
		if (block)
		{
			block(gesture);
		}
	}
}
- (void)gg_addLongPressActionWithBlock:(GestureActionBlock)block
{
	UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerLongPressGestureKey);
	if (!gesture)
	{
		gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForLongPressGesture:)];
		[self addGestureRecognizer:gesture];
		objc_setAssociatedObject(self, &kActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
	}
	objc_setAssociatedObject(self, &kActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY);
}

- (void)handleActionForLongPressGesture:(UITapGestureRecognizer*)gesture
{
	if (gesture.state == UIGestureRecognizerStateRecognized)
	{
		GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerLongPressBlockKey);
		if (block)
		{
			block(gesture);
		}
	}
}

@end

